package com.qf.service.impl;

import com.qf.dao.AddessRepository;
import com.qf.dao.UserMapper;
import com.qf.dao.UserRepository;
import com.qf.pojo.req.UserReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.Address;
import com.qf.pojo.vo.User;
import com.qf.service.UserService;
import com.qf.utils.JwtUtils;
import com.qf.utils.SendUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    UserRepository userRepository;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    SendUtils sendUtils;

    @Autowired
    UserMapper userMapper;
    String uuidname = "abcdefghijklmnopqrstuvwxyz";
    @Autowired
    AddessRepository addessRepository;

    @Override
    public BaseResp login(UserReq userreq) {
        BaseResp baseResp = new BaseResp();
        if (StringUtils.isEmpty(userreq.getPhone())) {
            baseResp.setCode(Integer.valueOf(201));
            baseResp.setMessage("手机号不能为空");
            return baseResp;
        }
        if ((StringUtils.isEmpty(userreq.getPassword())) && (StringUtils.isEmpty(userreq.getCode()))) {
            baseResp.setCode(Integer.valueOf(202));
            baseResp.setMessage("密码或验证码为空");
        }
        if (!(StringUtils.isEmpty(userreq.getPassword()))) {
            User byPhone = this.userRepository.findByPhone(userreq.getPhone());
            if (byPhone == null) {
                baseResp.setCode(Integer.valueOf(200));
                baseResp.setMessage("用户不存在");
                return baseResp;
            }
            if (byPhone.getPassword().equals(userreq.getPassword())) {
                UUID uuid = UUID.randomUUID();
                JwtUtils jwtUtils = new JwtUtils();
                Map map = new HashMap();
                map.put("id", byPhone.getId());
                map.put("userName", byPhone.getUserName());
                String token = jwtUtils.securtToken(map);
                this.redisTemplate.opsForValue().set(uuid.toString(), token);
                baseResp.setCode(Integer.valueOf(200));
                baseResp.setMessage("登陆成功");
                baseResp.setData(uuid);
                return baseResp;
            }
            baseResp.setCode(Integer.valueOf(203));
            baseResp.setMessage("密码错误");
            return baseResp;
        }

        if (!(StringUtils.isEmpty(userreq.getCode()))) {
            if (this.redisTemplate.hasKey(userreq.getPhone()).booleanValue()) {
                Object o = this.redisTemplate.opsForValue().get(userreq.getPhone());
                if (o.toString().equals(userreq.getCode())) {
                    User byPhone = this.userRepository.findByPhone(userreq.getPhone());
                    if (byPhone != null) {
                        HashMap map = new HashMap();
                        map.put("id", byPhone.getId());
                        map.put("userName", byPhone.getUserName());
                        UUID uuid = UUID.randomUUID();
                        JwtUtils jwtUtils = new JwtUtils();
                        String token = jwtUtils.securtToken(map);
                        this.redisTemplate.opsForValue().set(uuid.toString(), token);
                        baseResp.setData(uuid);
                        baseResp.setCode(Integer.valueOf(200));
                        baseResp.setMessage("登陆成功");
                        return baseResp;
                    }
                    StringBuffer stringBuffer = new StringBuffer();
                    for (int i = 0; i < 10; ++i) {
                        int i1 = (int) (Math.random() * 26.0D);
                        String s = this.uuidname.substring(i1, i1 + 1);
                        stringBuffer.append(s);
                    }
                    User user = new User();
                    user.setPhone(userreq.getPhone());
                    user.setUserName(stringBuffer.toString());
                    this.userMapper.insert(user);

                    HashMap map = new HashMap();
                    map.put("id", user.getId());
                    map.put("userName", stringBuffer);
                    UUID uuid = UUID.randomUUID();
                    JwtUtils jwtUtils = new JwtUtils();
                    String token = jwtUtils.securtToken(map);
                    this.redisTemplate.opsForValue().set(uuid.toString(), token);
                    baseResp.setData(uuid);
                    baseResp.setCode(Integer.valueOf(200));
                    baseResp.setMessage("登陆成功");
                    return baseResp;
                }

                baseResp.setMessage("验证码错误，请重新输入");
                baseResp.setCode(Integer.valueOf(205));
                return baseResp;
            }

            baseResp.setCode(Integer.valueOf(204));
            baseResp.setMessage("请先获取验证码");
            return baseResp;
        }
        return null;
    }

    @Override
    public BaseResp sendphone(String phone) {
        BaseResp baseResp = this.sendUtils.sendPhone(phone);
        return baseResp;
    }

    @Override
    public BaseResp findone(String phone) {
        BaseResp baseResp = new BaseResp();
        User byPhone = this.userRepository.findByPhone(phone);
        if (byPhone == null) {
            baseResp.setCode(Integer.valueOf(201));
            return baseResp;
        }
        baseResp.setCode(Integer.valueOf(200));
        baseResp.setData(byPhone);
        return baseResp;
    }

    @Override
    public BaseResp updateinformation(User user) {
        this.userMapper.updateinformation(user);
        BaseResp baseResp = new BaseResp();
        baseResp.setMessage("修改成功");
        baseResp.setCode(Integer.valueOf(200));
        return baseResp;
    }

    @Override
    public BaseResp findAddress(Integer id) {
        List address = this.userMapper.findAddress(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(Integer.valueOf(200));
        baseResp.setData(address);
        return baseResp;
    }


    @Override
    public BaseResp addORupdate(Address address) {
        BaseResp baseResp = new BaseResp();

        if (!StringUtils.isEmpty(address.getId())) {
            baseResp.setCode(200);
            baseResp.setMessage("修改成功");
            addessRepository.saveAndFlush(address);
            return baseResp;
        }
        baseResp.setMessage("添加成功");
        baseResp.setCode(200);
        addessRepository.saveAndFlush(address);
        return baseResp;
    }

    @Override
    public BaseResp deleteAddess(Integer id) {
        addessRepository.deleteById(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setMessage("删除成功");
        return baseResp;
    }
}
